package com.wes.dsptest.dspcontrol;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.core.content.ContextCompat;

import com.wes.dsptest.R;
import com.xw.repo.BubbleSeekBar;

public class VolumeSliderView extends FrameLayout {

    private TextView mTitle;
    private TextView mCurrentValue;
    private BubbleSeekBar sbVol;

    private VolumeSliderChangeListener volumeListener;
    private BubbleSeekBar.OnProgressChangedListener listener = new BubbleSeekBar.OnProgressChangedListener() {
        @Override
        public void onProgressChanged(BubbleSeekBar bubbleSeekBar, int progress, float progressFloat, boolean fromUser) {
            if (volumeListener != null) {
                volumeListener.onChange(progress, fromUser);
            }
        }

        @Override
        public void getProgressOnActionUp(BubbleSeekBar bubbleSeekBar, int progress, float progressFloat) {

        }

        @Override
        public void getProgressOnFinally(BubbleSeekBar bubbleSeekBar, int progress, float progressFloat, boolean fromUser) {

        }
    };


    public VolumeSliderView(Context context) {
        super(context);
        init();
    }

    public VolumeSliderView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public VolumeSliderView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public VolumeSliderView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private void init() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.audio_type_view, this, false);
        mTitle = view.findViewById(R.id.title);
        mCurrentValue = view.findViewById(R.id.current_value);
        sbVol = view.findViewById(R.id.seek_bar);
        sbVol.setOnProgressChangedListener(listener);
        sbVol.setBubbleColor(ContextCompat.getColor(getContext(), R.color.color_blue));
        sbVol.setThumbColor(ContextCompat.getColor(getContext(), R.color.color_blue));
        sbVol.setSecondTrackColor(ContextCompat.getColor(getContext(), R.color.color_red));

        addView(view);
    }


    public void setMaxVolume(int maxVolume) {
        sbVol.getConfigBuilder().max((float) maxVolume).build();
    }

    public void setMinVolume(int minVolume) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            sbVol.getConfigBuilder().min((float) minVolume).build();
        }
    }

    public void setVolumeName(CharSequence string) {
        mTitle.setText(string);
        sbVol.setContentDescription(getContext().getString(R.string.volume_switch, string));
    }

    public void setCurrentVolume(int progress) {
        setCurrentVolume(progress, false);
    }


    public void updateProgressText(int progress) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            mCurrentValue.setText("" + (progress - sbVol.getMin()) + "/" + (sbVol.getMax() - sbVol.getMin()));
        } else {
            mCurrentValue.setText("" + progress + "/" + sbVol.getMax());
        }
    }

    public void updateAC8015ProgressText(int progress) {
            mCurrentValue.setText("" + progress + "dB/" + sbVol.getMax());
    }

    public void setCurrentVolume(int progress, boolean animated) {
        sbVol.setProgress(progress);
        updateAC8015ProgressText(progress);

    }

    public void setListener(VolumeSliderChangeListener volumeListener) {
        this.volumeListener = volumeListener;
    }

    public interface VolumeSliderChangeListener {
        void onChange(int volume, boolean fromUser);
    }
}
